package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.dao.GreenHouseDao;
import edu.ynu.se.xiecheng.achitectureclass.dto.GreenHouseDTO;
import edu.ynu.se.xiecheng.achitectureclass.entity.GreenHouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GreenHouseService extends LogicService<GreenHouseDao, GreenHouse, Long> {

    public GreenHouseService(@Autowired GreenHouseDao greenHouseDao) {
        super(greenHouseDao);
    }

    public List<GreenHouseDTO> getAllGreenHousesDTO() {
        List<GreenHouse> greenHouses = getAll();
        return greenHouses.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private GreenHouseDTO convertToDTO(GreenHouse greenHouse) {
        GreenHouseDTO greenHouseDTO = new GreenHouseDTO();
        greenHouseDTO.setId(greenHouse.getId());
        greenHouseDTO.setName(greenHouse.getName());
        greenHouseDTO.setAddress(greenHouse.getAddress());
        greenHouseDTO.setArea(greenHouse.getArea());
        greenHouseDTO.setCreatedAt(greenHouse.getCreatedAt());
        return greenHouseDTO;
    }
}
